import { ref } from 'vue'
import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr'
import type { CategoryResponseData } from '@/api/product/attr/type'

const useCategoryStore = defineStore('Category', () => {
  // 存储一级分类的数据
  const c1Arr = ref<Array<any>>([1, 1])
  // 存储一级分类的ID
  const c1Id = ref<string>('')
  // 存储对应一级分类下二级分类的数据
  const c2Arr = ref<Array<any>>([])
  // 存储二级分类的ID
  const c2Id = ref<string>('')
  // 存储三级分类的数据
  const c3Arr = ref<Array<any>>([])
  // 存储三级分类的ID
  const c3Id = ref<string>('')

  // 获取一级分类的方法
  const getC1 = async () => {
    // 发请求获取一级分类的数据
    const result: CategoryResponseData = await reqC1()
    if (result.code == 200) {
      c1Arr.value = result.data
    }
  }

  // 获取二级分类的数据
  const getC2 = async () => {
    // 获取对应一级分类的下二级分类的数据
    const result: CategoryResponseData = await reqC2(c1Id.value)
    if (result.code == 200) {
      c2Arr.value = result.data
    }
  }

  // 获取三级分类的数据
  const getC3 = async () => {
    const result: CategoryResponseData = await reqC3(c2Id.value)
    if (result.code == 200) {
      c3Arr.value = result.data
    }
  }
  const reset = () => {
    c1Arr.value = []
    c1Id.value = []
    c2Arr.value = []
    c2Id.value = []
    c3Arr.value = []
    c3Id.value = []
  }

  return {
    c1Arr,
    c1Id,
    c2Arr,
    c2Id,
    c3Arr,
    c3Id,
    getC1,
    getC2,
    getC3,
    reset
  }
})

export default useCategoryStore
